package com.dinghui.salary.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dinghui.basic.information.management.service.MenuService;
import com.dinghui.basic.information.management.vo.MenuVO;
import com.dinghui.salary.enums.ResponseStatusEnum;
import com.dinghui.salary.exception.CommonException;
import com.dinghui.salary.pojo.dto.LoginDTO;
import com.dinghui.salary.pojo.vo.LoginUserVO;
import com.dinghui.salary.utils.*;
import com.dinghui.user.management.service.UserBiz;
import com.dinghui.user.management.vo.Users;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * @author tanrentao
 * @date 2021-09-06
 * @desc 用户登录
 */
@RestController
@RequestMapping("/user")
@Api(value = "LoginController", tags = "用户登录")
public class LoginController {

  /*  @Reference(version = "2.0.0")
    UserService userService;*/

    @Reference(check = false)
    private UserBiz userBiz;

    @Reference(check = false)
    private MenuService menuService;

    @Autowired
    private RedisOperator redisOperator;

    public static final String REDIS_USER_TOKEN = "redis_user_token";
    public static final String REDIS_USER_INFO = "redis_user_info";


    /*@ApiOperation("登录")
    @PostMapping("/login")
    public ApiResult loginIn(@RequestBody @Validated LoginDTO dto) throws Exception {
        User user = userService.login(dto);
        if (Objects.isNull(user)) {
            CommonException.display(ResponseStatusEnum.USER_NOT_EXIST_ERROR);
        }

        if (MD5Utils.getMD5Str(dto.getPassword()).equals(user.getPassword())) {
            CommonException.display(ResponseStatusEnum.USER_PASSWORD_ERROR);
        }

        LoginUserVO loginUserVO = new LoginUserVO();
        BeanUtils.copyProperties(user, loginUserVO);
        loginUserVO.setToken(JwtToken.makeToken(Long.valueOf(user.getUserId())));

        return ApiResult.success(loginUserVO);
    }*/

    @ApiOperation("登录")
    @PostMapping("/login")
    public ApiResult login(@RequestBody @Validated LoginDTO dto, HttpServletRequest request, HttpServletResponse response) throws Exception {

        Users user = userBiz.login(dto.getUsername(), null);

        if (Objects.isNull(user)) {
            CommonException.display(ResponseStatusEnum.USER_NOT_EXIST_ERROR);
        }

        if (MD5Utils.getMD5(dto.getPassword()).equals(user.getPass_word())) {
            CommonException.display(ResponseStatusEnum.USER_PASSWORD_ERROR);
        }

        String token = JwtToken.makeToken(Long.valueOf(user.getId()));
        redisOperator.set(REDIS_USER_TOKEN + ":" + user.getId(), token);
        redisOperator.set(REDIS_USER_INFO + ":" + user.getId(), JsonUtils.objectToJson(user));
        //redisOperator.set(token+":user", JsonUtils.objectToJson(user));

        // request.getSession().setAttribute("casSessionId", token);

        LoginUserVO loginUserVO = new LoginUserVO();
        BeanUtils.copyProperties(user, loginUserVO);
        loginUserVO.setToken(token);

        return ApiResult.success(loginUserVO);
    }

    @ApiOperation("页面跳转首页")
    @GetMapping("/loginIndex")
    public void loginIndex(HttpServletResponse response, String casSessionId) {
        Cookie cookie = new Cookie("casSessionId", casSessionId);
        response.addCookie(cookie);
    }
    // http://192.168.200.77:9992/api/user/loginIndex?casSessionId=11.d16581f7c5032947a96b82e7e163e7ed6bb6c2ad.2592000.1634439956
    // http://192.168.200.130/one-card-solution/user/login?casSessionId=11.d16581f7c5032947a96b82e7e163e7ed6bb6c2ad.2592000.1634439956&tex=admin&psw=dc483e80a7a0bd9ef71d8cf973673924

    // http://111.22.209.108:33444/qbbank/index?casSessionId=11.e3cfa20fd98a508827193437e8f939dd5fb57c1d.2592000.1634449169

    @ApiOperation("获取菜单")
    @GetMapping("/getMenu")
    public ApiResult getMenu() {
//        String userInfo = redisOperator.get(REDIS_USER_INFO + ":" + LocalUser.getUser().getId());
//        Users user = JsonUtils.jsonToPojo(userInfo, Users.class);

        Integer roleId = LocalUser.getUser().getRole_id();
        String projectId = "53"; // 项目id
        Integer pid = 0;
        List<MenuVO> menuList = menuService.getMenuByRoleId(roleId, projectId, pid);
        return ApiResult.success(menuList);
    }


    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public ApiResult logout(@RequestParam String userId, HttpServletRequest request, HttpServletResponse response) {
        redisOperator.del(REDIS_USER_TOKEN + ":" + userId);
        return ApiResult.success();
    }







}
